#include <stdio.h>
#include <string.h>
#define MAXLINE		80
#define NL		10
#define CR		13
 unsigned char line[MAXLINE+1];
 unsigned char line2[MAXLINE+1];
 unsigned char line3[MAXLINE+1];
 unsigned char line4[MAXLINE+1];
 int pos = 0;

print_line(a,b,c,d)
unsigned char *a;
unsigned char *b;
unsigned char *c;
unsigned char *d;
{
    line[pos] = *a;
    line2[pos] = *b;
    line3[pos] = *c;
    line4[pos] = *d;
    line[pos+1] = '\0';
    line2[pos+1] = '\0';
    line3[pos+1] = '\0';
    line4[pos+1] = '\0';
}

help_description()
{

printf("\n");
printf("usage: dumpix [args] input_file\n\n");
printf("Non-printable characters will be displayed with their hexidecimal\n");
printf(" values unless the octal flag is specified\n");
printf("Output will be sent to standard out\n\n");
printf(" -dd    - show the hexidecimal values of all characters\n");
printf(" -octal - output the octal values for non-printable characters\n");
printf(" -cr    - show all occurances of carriage return characters\n");
printf(" -lf    - show all occurances of line feed characters\n");
printf(" -crlf  - show all occurances of carriage return and line feed characters\n");
printf(" -help  - display this screen\n\n");

}


main(argc,argv)
int argc;
char *argv[];
{
 long l=0;
 int c;
 int cr_replace,lf_replace;
 unsigned char letter;
 char string[4];
 char *filename;
 int dd=0;
 int add=0;
 int octal =0;
 int cr_flag =0;
 int lf_flag =0;
 FILE *infile = stdin;
 while(--argc) {
    if (sscanf(argv[argc],"-c%d",&cr_replace));
    else if (!strcmp(argv[argc],"-dd"))
        add=1;
    else if (!strcmp(argv[argc],"-octal"))
        octal=1;
    else if (!strcmp(argv[argc],"-cr"))
        cr_flag=1;
    else if (!strcmp(argv[argc],"-lf"))
        lf_flag=1;
    else if (!strcmp(argv[argc],"-crlf"))
      {
        cr_flag=1;
        lf_flag=1;
      }
    else if (!strcmp(argv[argc],"-help"))
      {
	help_description();
	exit(0);
      }
    else if(sscanf(argv[argc],"-l%d",&lf_replace));
    else if (argv[argc][0] == '-')
     {
	 printf("\n%s: Invalid argument %s\n",argv[0],argv[argc]);
	 help_description();
	 exit(1);
    }
    else 
	{
	    if (infile != stdin)
	     {
		 printf("\n%s: Too many input files\n",argv[0],argv[argc]);
		 help_description();
		 exit(1);
	     }
	    if ((infile=fopen(argv[argc], "r")) == NULL)
	     {
		 printf("\n%s: Unable to open file %s\n",argv[0],argv[argc]);
		 help_description();
		 exit(1);
	     }
	}
	
 }
 while ((c=getc(infile)) != EOF) {
     l++;
    letter = c;
    if(octal) 
        sprintf(string,"%03o",letter);
    else
	sprintf(string,"%02x",letter); 
    if (c > 31 && c < 127)
	print_line(&letter,add ? &string[0] : " ",add ? &string[1] :" ", add ? &string[2]:" ");
    else if (c == NL) {
/*	print_line(" "," "," "," ");
	print_line(" ","H","R"," ");
	dd = 1;  */
	if (lf_flag)
	    {
		print_line(" ","L","F"," ");
		dd = 1;
	    }
	pos = MAXLINE;
    }
    else if (c==CR) {
/*	print_line(" "," "," "," ");
	print_line(" ","C","R"," ");
	dd = 1; 
	pos = MAXLINE;*/
	if (cr_flag)
	    {
		print_line(" ","C","R"," ");
		dd = 1;
	    }
	else
		pos--;
    }
    else {
	dd = 1;
	print_line(" ",&string[0],&string[1],&string[2]);
    }
    if (++pos < MAXLINE);
    else {
	pos = 0;
	printf("%s\n",line);
	if (dd || add) 
	    if(octal)
	        printf("%s\n%s\n%s\n",line2,line3,line4);
	    else
	        printf("%s\n%s\n",line2,line3);
	dd = 0;
	line[0] = '\0';
	line2[0] = '\0';
	line3[0] = '\0';
	line4[0] = '\0';
    }
 }
 printf("%s\n",line);
 if (dd || add) 
    if(octal)
        printf("%s\n%s\n%s\n",line2,line3,line4);
    else
        printf("%s\n%s\n",line2,line3);
}
